package com.gk.study.controller;

import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.Order;
import com.gk.study.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询订单信息
     *
     * @param
     * @return
     */
    @GetMapping(value = "/list")
    public APIResponse list() {
        log.info("开始查订单信息");
        List<Order> list = orderService.getOrderList();
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional
    public APIResponse create( String userId,  String thingId) throws IOException {
        int number = orderService.createOrderOne(userId,thingId);
        return new APIResponse(ResponeCode.SUCCESS, "创建成功",number);
    }
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @Transactional
    public APIResponse cancelOrder(
            @RequestParam("userId") String userId,
            @RequestParam("thingId") String thingId) throws IOException {
        orderService.cancelOrderOne(userId, thingId);
        return new APIResponse(ResponeCode.SUCCESS, "取消成功");
    }
    // 用户订单
    @RequestMapping(value = "/userOrderList", method = RequestMethod.GET)
    public APIResponse userOrderList(String userId, String status){
        List<Order> list =  orderService.getUserOrderList(userId, status);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }
    //删除订单
    @PostMapping("/delete")
    public APIResponse delete(@RequestParam String id){
        int longId = Integer.parseInt(id); // 转换为 Long
     int result =  orderService.deleteOneById(longId);
        System.out.println(longId);
     return new APIResponse<>(ResponeCode.SUCCESS,result);
    }
}
